package edu.chinasoft.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.annotation.WebServlet;

import org.apache.log4j.Logger;

/**
 * 
 * @author cjogng
 * @date 2020年1月14日:下午3:36:31
 * @version V1.0
 * @Description: 验证码
 */
@WebServlet(name = "myResponse09", value = "/servlet/response09")
public class Response09 extends HttpServlet {
	private static Logger logger = Logger.getLogger(Response09.class);

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" Response09 >doGet start.......");

		int width = 200;
		int heigth = 40;
		String imageType = "jpg";
		
		//设置验证码不缓存
		response.setDateHeader("Expires", 0);
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		
		

		// 告诉浏览器数据类型为jpg
		ServletContext servletContext = this.getServletContext();
		String mimeType = servletContext.getMimeType(imageType);
		response.setHeader("Content-Type", mimeType);

		OutputStream out = response.getOutputStream();

		// 步骤1:生成验证码图片 （j2se）
		BufferedImage image = new BufferedImage(width, heigth,
				BufferedImage.TYPE_INT_RGB);
		// 获取画布的画笔
		Graphics graphics = image.getGraphics();

		// 设置画布的背景颜色是白色
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, width, heigth);

		// 设置边框
		graphics.setColor(Color.BLACK);
		graphics.drawRect(0, 0, width - 2, heigth - 2);

		// 绘制干扰线
		graphics.setColor(Color.BLUE);

		int x1;
		int y1;
		int x2;
		int y2;
		Random random = new Random();
		for (int i = 1; i <= 4; i++) {
			x1 = random.nextInt(width);
			x2 = random.nextInt(width);
			y1 = random.nextInt(heigth);
			y2 = random.nextInt(heigth);
			graphics.drawLine(x1, y1, x2, y2);
		}

		// 绘制内容
		graphics.setColor(Color.RED);
		graphics.setFont(new Font("宋体", Font.BOLD, 18));

		String content = "适得府君书看风景时空房间开始的房价快速福建省客服经理是否就是开了房间辣刷卡机福克斯尖峰时刻福建省非计算机房卡是否加快速度abcewdklsdfs2321428934823";

		int x = width / 6;
		int y = heigth * 2 / 3;
		String verificationCode="";
		
		for (int i = 1; i <= 4; i++) {

			int index = random.nextInt(content.length());
			char element = content.charAt(index);
			verificationCode+=element;
			graphics.drawString(element + "", x, y);
			x += width / 6;

		}
		logger.debug("验证码为："+verificationCode+"。");
		
		HttpSession session = request.getSession();
		
		session.setAttribute("verificationcodeserver", verificationCode);
		
		
		

		// 步骤2：把图片显示在浏览器
		ImageIO.write(image, imageType, out);

		logger.debug(" Response09 >doGet end.......");
	}

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" Response09 >doPost start.......");
		this.doGet(request, response);

		logger.debug(" Response09 >doPost end.......");
	}

}